# setwd("~/Dropbox/T&G project survey/Ukraine/replication_JOP")
# libraries
library(tidyverse)
library(scales)
library(haven)
library(patchwork)
library(paletteer)
library(lubridate)
source("func.R")

# ggplot defaults: geoms theme
update_geom_defaults("text", list(family = "Archivo Narrow"))
update_geom_defaults("label", list(family = "Archivo Narrow"))
theme_set(theme_nice())

# Data (raw file)
dfraw = read_rds("data/survey_ukraine_items.rds")

## Support for sanctions

df_ukraine = dfraw %>%
  dplyr::select(response_id, Q62_1, Q62_2, Q62_3) %>%
  # rename("sanctions" = Q62_1, "milsupport" = Q62_2, "stay_out" = Q62_3) %>%
  pivot_longer(-response_id, names_to = "var", values_to = "agree") %>%
  mutate(agree = as.factor(agree)) %>%
  mutate(var2 = recode(var,
    "Q62_1" = "Favor sanctions against Russia",
    "Q62_2" = "Favor military support for Ukraine",
    "Q62_3" = "Spain should stay out of Ukraine"))
levels(df_ukraine$agree) = c("Strongly\nagree", "Agree", "Neither\nagree nor\ndisagree",
  "Disagree", "Strongly\ndisagree")

p = ggplot(df_ukraine, aes(x = agree)) +
  geom_bar(color = "white") +
  labs(x = "", y = "") +
  facet_wrap(~var2)

ggsave("figures/ukraine_support.pdf", width = 7.5, height = 3.5, device = cairo_pdf)

## Support for sanctions (EUK/CAT vs REST of SPAIN)

df_ukraine_eukcat = dfraw %>%
  mutate(eukcat = ifelse(ccaa %in% c(9, 16),
    "Catalonia/Basque Country", "Rest of Spain")) %>%
  dplyr::select(eukcat,
    `Favor sanctions against Russia` = Q62_1,
    `Favor military support for Ukraine` = Q62_2,
    `Spain should stay out of Ukraine` = Q62_3) %>%
  # rename("sanctions" = Q62_1, "milsupport" = Q62_2, "stay_out" = Q62_3) %>%
  pivot_longer(-eukcat) %>%
  mutate(value = recode(as_factor(value),
    "Muy de acuerdo" = "Strongly agree",
    "Ni de acuerdo ni en desacuerdo" = "Neither agree nor disagree",
    "De acuerdo" = "Agree",
    "Nada de acuerdo" = "Strongly disagree",
    "Poco de acuerdo" = "Disagree")) %>%
  group_by(name, eukcat, value) %>%
  tally() %>%
  mutate(pct = n/sum(n))


p = ggplot(df_ukraine_eukcat, aes(x = value, y = pct, group = eukcat, fill = eukcat)) +
  geom_col(position = "dodge2") +
  facet_wrap(vars(name), scales = "free") +
  scale_y_continuous(labels = scales::percent) +
  scale_x_discrete(labels = label_wrap(10)) +
  labs(x = "", y = "", fill = "") +
  theme(legend.position = "bottom")
ggsave("figures/ukraine_support_eukcat.pdf", width = 8, height = 3.5, device = cairo_pdf)


## Support over time
df = dfraw %>%
  select(ts, contains("Q62_")) %>%
  mutate(across(contains("Q62"), ~ 6 - .)) %>%
  mutate(date = mdy_hms(ts),
         month = month(date),
         day = day(date),
         hour = hour(date)) %>%
  mutate(time = cut(date, "day"))

# how many surveys per day
# df %>% group_by(time) %>% tally()

# Plot formatted data
pdat = df %>%
  select(time, contains("Q62_")) %>%
  pivot_longer(-time) %>%
  # rename outcomes
  mutate(clean = case_when(
    name == "Q62_1" ~ "Favor sanctions against Russia",
    name == "Q62_2" ~ "Favor military support for Ukraine",
    name == "Q62_3" ~ "Spain should stay out of Ukraine")) %>%
  group_by(time, clean) %>%
  summarise(mean = mean(value),
            sd = sd(value),
            n = n()) %>%
  # drop days with less than 10 responses
  filter(n >= 10) %>%
  # calculate margin of error
  mutate(margin = qt(0.975,df=n-1)*sd/sqrt(n)) %>%
  mutate(time = ymd(time))


support_plot = ggplot(pdat, aes(x = time, y = mean,
                 ymin = mean - margin,
             ymax = mean + margin)) +
  facet_wrap(vars(clean)) +
  geom_pointrange() +
  geom_smooth(size = 1.5, fill = "blue", alpha = .2) +
  labs(x = NULL, subtitle = "Only including days with at least 100 responses",
    y = "Average support for policy \n(1 = Strong disapprove, 5 = strong approve)")

ggsave("figures/ukraine_support_time_series.pdf", width = 7, height = 5, device = cairo_pdf)
